//
//  SetViewController.m
//  Contractor
//
//  Created by 金鑫 on 16/6/6.
//  Copyright © 2016年 star j. All rights reserved.
//

#import "SetViewController.h"
#import "CustomTextField.h"
#import "AdressLTViewController.h"
#import "ServiceLTViewController.h"
#import "AppDelegate.h"
#import "MainTabBarViewController.h"
#import "MBProgressHUD.h"
#import "CONST.h"

@interface SetViewController ()<UITextFieldDelegate>
{
    CustomTextField *account ;
    CustomTextField *idCard ;
    UITextField *liveadress ;
    UIScrollView *scrollView ;
    NSInteger sexStatus ;
    UIButton *manBtn ;
    UIButton *womanBtn ;
    UILabel *locationStrLabel ;
    UILabel *serviceStrLabel ;
    UIButton *saveBtn ;
}

@end

@implementation SetViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.view.backgroundColor = [UIColor whiteColor] ;
    self.extendedLayoutIncludesOpaqueBars = NO;
    self.edgesForExtendedLayout = UIRectEdgeBottom | UIRectEdgeLeft | UIRectEdgeRight;
    self.navigationItem.title = @"填写个人信息" ;
    sexStatus = 1 ;
    [self createRZView] ;
}

- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBarHidden = NO ;
}

- (void)viewDidAppear:(BOOL)animated
{
    if (self.location_model) {
        if ([_location_model.location1_cn isEqualToString:_location_model.location2_cn]) {
            locationStrLabel.text = [NSString stringWithFormat:@"%@ %@",_location_model.location1_cn,_location_model.location3_cn] ;
        }
        else{
            locationStrLabel.text = [NSString stringWithFormat:@"%@ %@ %@",_location_model.location1_cn,_location_model.location2_cn,_location_model.location3_cn] ;
        }
    }
    if (self.service_model) {
        if ([_service_model.location1_cn isEqualToString:_service_model.location2_cn]) {
            serviceStrLabel.text = [NSString stringWithFormat:@"%@ %@",_service_model.location1_cn,_service_model.location3_cn] ;
        }
        else{
            serviceStrLabel.text = [NSString stringWithFormat:@"%@ %@ %@",_service_model.location1_cn,_service_model.location2_cn,_service_model.location3_cn] ;
        }
    }
}

- (void)viewWillDisappear:(BOOL)animated
{
    self.navigationController.navigationBarHidden = YES ;
}


- (void)createRZView
{
    
    scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, __kScreenWidth, __kScreenHeight-60)] ;
    scrollView.backgroundColor = [UIColor whiteColor] ;
    scrollView.showsVerticalScrollIndicator = NO ;
    scrollView.bounces = NO ;
    [self.view addSubview:scrollView] ;
    
    UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyBoard:)];
    tapGestureRecognizer.numberOfTapsRequired = 1;
    [scrollView addGestureRecognizer: tapGestureRecognizer];   //只需要点击非文字输入区域就会响应hideKeyBoard

    
    //账号输入框
    account = [[CustomTextField alloc] initWithFrame:CGRectMake(20, 0, __kScreenWidth-40, 50)] ;
    [account setBackgroundColor:[UIColor clearColor]] ;
    account.placeholder = @"姓名" ;
    account.delegate = self ;
    
    //账号最左侧图片
    UIImageView *leftImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 24, 24)] ;
    [leftImage setImage:[UIImage imageNamed:@"partner.png"]] ;
    account.leftView = leftImage ;
    account.leftViewMode = UITextFieldViewModeAlways ;
    account.textAlignment = NSTextAlignmentLeft ;
    account.autocapitalizationType = UITextAutocapitalizationTypeNone ;//首字母不大写
    account.autocorrectionType = UITextAutocorrectionTypeNo ;//是否纠错
    [scrollView addSubview:account] ;
    
    //账号最右边图片
//    UIButton *rightImage2 = [[UIButton alloc] initWithFrame:CGRectMake(__kScreenWidth - 44, account.frame.origin.y + 13, 24, 24)] ;
    
    UIButton *rightImage2 = [[UIButton alloc] initWithFrame:CGRectMake(__kScreenWidth - 57, account.frame.origin.y, 50, 50)] ;
    
    [rightImage2 setContentMode:UIViewContentModeCenter];
    [rightImage2 setImageEdgeInsets:UIEdgeInsetsMake(13, 13, 13, 13)];
    
    [rightImage2 setImage:[UIImage imageNamed:@"login_del_username_normal_icon.png"] forState:UIControlStateNormal] ;
    [rightImage2 setImage:[UIImage imageNamed:@"login_del_username_press_icon.png"] forState:UIControlStateHighlighted] ;
    [rightImage2 addTarget:self action:@selector(cleartext) forControlEvents:UIControlEventTouchUpInside] ;
    [scrollView addSubview:rightImage2] ;
    
    //分割线1
    UIImageView *line1 = [[UIImageView alloc] initWithFrame:CGRectMake(10, account.frame.origin.y + account.frame.size.height, __kScreenWidth - 20, 1)] ;
    [line1 setImage:[UIImage imageNamed:@"layout_small_line_bg.png"]] ;
    [scrollView addSubview:line1] ;
    
    //性别lable
    UILabel *sexLabel = [self creatlabel:14.0f color:[UIColor grayColor]] ;
    sexLabel.text = @"性别" ;
    CGSize sexLength = [self lengthwithLabel:sexLabel withsize:14.0f] ;
    sexLabel.frame = CGRectMake(20, line1.frame.origin.y + line1.frame.size.height + 18, sexLength.width, 16.0f) ;
    [scrollView addSubview:sexLabel] ;
    
    //性别男btn
    manBtn = [UIButton buttonWithType:UIButtonTypeCustom] ;
    [manBtn setImage:[UIImage imageNamed:@"radiobutton_checked_img.png"] forState:UIControlStateNormal] ;
    manBtn.frame = CGRectMake(sexLabel.frame.origin.x + sexLabel.frame.size.width + 10, line1.frame.origin.y + line1.frame.size.height + 12, 26, 26) ;
    [manBtn addTarget:self action:@selector(selectMan) forControlEvents:UIControlEventTouchUpInside] ;
    [scrollView addSubview:manBtn] ;
    
    //男label
    UILabel *manLabel = [self creatlabel:15.0f color:[UIColor blackColor]] ;
    manLabel.text = @"男" ;
    CGSize manLength = [self lengthwithLabel:manLabel withsize:15.0f] ;
    manLabel.frame = CGRectMake(manBtn.frame.origin.x + manBtn.frame.size.width + 3, line1.frame.origin.y + line1.frame.size.height + 17.5, manLength.width, 17.0f) ;
    [scrollView addSubview:manLabel] ;
    
    //性别女btn
    womanBtn = [UIButton buttonWithType:UIButtonTypeCustom] ;
    [womanBtn setImage:[UIImage imageNamed:@"radiobutton_normal_img.png"] forState:UIControlStateNormal] ;
    womanBtn.frame = CGRectMake(manLabel.frame.origin.x + manLabel.frame.size.width + 15, line1.frame.origin.y + line1.frame.size.height + 12, 26, 26) ;
    [womanBtn addTarget:self action:@selector(selectWoman) forControlEvents:UIControlEventTouchUpInside] ;
    [scrollView addSubview:womanBtn] ;
    
    //女label
    UILabel *womanLabel = [self creatlabel:15.0f color:[UIColor blackColor]] ;
    womanLabel.text = @"女" ;
    womanLabel.frame = CGRectMake(womanBtn.frame.origin.x + womanBtn.frame.size.width + 3, line1.frame.origin.y + line1.frame.size.height + 17.5, manLength.width, 17.0f) ;
    [scrollView addSubview:womanLabel] ;
    
    //分割线2
    UIImageView *line2 = [[UIImageView alloc] initWithFrame:CGRectMake(10, line1.frame.origin.y + line1.frame.size.height + 50, __kScreenWidth - 20, 1)] ;
    [line2 setImage:[UIImage imageNamed:@"layout_small_line_bg.png"]] ;
    [scrollView addSubview:line2] ;
    
    //身份证输入框
    idCard = [[CustomTextField alloc] initWithFrame:CGRectMake(20, line2.frame.origin.y + line2.frame.size.height, __kScreenWidth-40, 50)] ;
    [idCard setBackgroundColor:[UIColor clearColor]] ;
    idCard.placeholder = @"身份证" ;
    idCard.delegate = self ;
    
    //身份证最左侧图片
    UIImageView *idleftImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 24, 24)] ;
    [idleftImage setImage:[UIImage imageNamed:@"idcard.png"]] ;
    idCard.leftView = idleftImage ;
    idCard.leftViewMode = UITextFieldViewModeAlways ;
    idCard.textAlignment = NSTextAlignmentLeft ;
    idCard.autocapitalizationType = UITextAutocapitalizationTypeNone ;//首字母不大写
    idCard.autocorrectionType = UITextAutocorrectionTypeNo ;//是否纠错
    [scrollView addSubview:idCard] ;
    
    //账号最右边图片
    //UIButton *idrightImage = [[UIButton alloc] initWithFrame:CGRectMake(__kScreenWidth - 44, idCard.frame.origin.y + 13, 24, 24)] ;
    
    UIButton *idrightImage = [[UIButton alloc] initWithFrame:CGRectMake(__kScreenWidth - 57, idCard.frame.origin.y, 50, 50)] ;
    [idrightImage setContentMode:UIViewContentModeCenter];
    [idrightImage setImageEdgeInsets:UIEdgeInsetsMake(13, 13, 13, 13)];
    
    [idrightImage setImage:[UIImage imageNamed:@"login_del_username_normal_icon.png"] forState:UIControlStateNormal] ;
    [idrightImage setImage:[UIImage imageNamed:@"login_del_username_press_icon.png"] forState:UIControlStateHighlighted] ;
    [idrightImage addTarget:self action:@selector(clearidtext) forControlEvents:UIControlEventTouchUpInside] ;
    [scrollView addSubview:idrightImage] ;
    
    //分割线3
    UIImageView *line3 = [[UIImageView alloc] initWithFrame:CGRectMake(10, idCard.frame.origin.y + idCard.frame.size.height, __kScreenWidth - 20, 1)] ;
    [line3 setImage:[UIImage imageNamed:@"layout_small_line_bg.png"]] ;
    [scrollView addSubview:line3] ;

    //所在区域view
    UIView *locationView = [[UIView alloc] initWithFrame:CGRectMake(0, line3.frame.origin.y + line3.frame.size.height, __kScreenWidth, 50)] ;
    locationView.backgroundColor = [UIColor whiteColor] ;
    locationView.userInteractionEnabled = YES ;
    [scrollView addSubview:locationView] ;
    
    UITapGestureRecognizer *locationtap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(pushLTAdress)];
    locationtap.numberOfTapsRequired = 1;
    [locationView addGestureRecognizer:locationtap];
    
    //所在区域lable
    UILabel *locationLabel = [self creatlabel:14.0f color:[UIColor grayColor]] ;
    locationLabel.text = @"所在区域" ;
    CGSize locationLength = [self lengthwithLabel:locationLabel withsize:14.0f] ;
    locationLabel.frame = CGRectMake(20, 18, locationLength.width, 16.0f) ;
    [locationView addSubview:locationLabel] ;
    
    //所在区域具体label
    locationStrLabel = [self creatlabel:16.0f color:[UIColor blackColor]] ;
    locationStrLabel.text = @"省/市/区" ;
    locationStrLabel.frame = CGRectMake(locationLabel.frame.origin.x + locationLabel.frame.size.width + 10, 17, __kScreenWidth - 100, 18.0f) ;
    [locationView addSubview:locationStrLabel] ;
    
    //右箭头图片
    UIImageView *nextView = [[UIImageView alloc] initWithFrame:CGRectMake(__kScreenWidth-40, 7.5 , 25, 35)] ;
    [nextView setImage:[UIImage imageNamed:@"page_access_next_icon.png"]] ;
    [locationView addSubview:nextView] ;
    
    //分割线4
    UIImageView *line4 = [[UIImageView alloc] initWithFrame:CGRectMake(10, locationView.frame.origin.y + locationView.frame.size.height, __kScreenWidth - 20, 1)] ;
    [line4 setImage:[UIImage imageNamed:@"layout_small_line_bg.png"]] ;
    [scrollView addSubview:line4] ;
    
    //详细地址
    liveadress = [[UITextField alloc] initWithFrame:CGRectMake(0, line4.frame.origin.y + line4.frame.size.height, __kScreenWidth, 50)] ;
    liveadress.placeholder = @"详细地址(如门牌号等)" ;
    liveadress.delegate = self ;
    liveadress.autocapitalizationType = UITextAutocapitalizationTypeNone ;//首字母不大写
    liveadress.autocorrectionType = UITextAutocorrectionTypeNo ;//是否纠错
    liveadress.textAlignment = NSTextAlignmentCenter ;
    [scrollView addSubview:liveadress] ;
    
    //分割线5
    UIImageView *line5 = [[UIImageView alloc] initWithFrame:CGRectMake(10, liveadress.frame.origin.y + liveadress.frame.size.height, __kScreenWidth - 20, 1)] ;
    [line5 setImage:[UIImage imageNamed:@"layout_small_line_bg.png"]] ;
    [scrollView addSubview:line5] ;
    
    
    //服务区域view
    UIView *serviceView = [[UIView alloc] initWithFrame:CGRectMake(0, line5.frame.origin.y + line5.frame.size.height, __kScreenWidth, 50)] ;
    serviceView.backgroundColor = [UIColor whiteColor] ;
    serviceView.userInteractionEnabled = YES ;
    [scrollView addSubview:serviceView] ;
    UITapGestureRecognizer *servicetap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(pushService)];
    servicetap.numberOfTapsRequired = 1;
    [serviceView addGestureRecognizer:servicetap];
    
    //服务区域lable
    UILabel *serviceLabel = [self creatlabel:14.0f color:[UIColor grayColor]] ;
    serviceLabel.text = @"服务区域" ;
    CGSize serviceLength = [self lengthwithLabel:serviceLabel withsize:14.0f] ;
    serviceLabel.frame = CGRectMake(20, 18, serviceLength.width, 16.0f) ;
    [serviceView addSubview:serviceLabel] ;
    
    //服务区域具体label
    serviceStrLabel = [self creatlabel:16.0f color:[UIColor blackColor]] ;
    serviceStrLabel.text = @"省/市/区" ;
    serviceStrLabel.frame = CGRectMake(serviceLabel.frame.origin.x + serviceLabel.frame.size.width + 10, 17, __kScreenWidth - 100, 18.0f) ;
    [serviceView addSubview:serviceStrLabel] ;
    
    //右箭头图片
    UIImageView *nextView2 = [[UIImageView alloc] initWithFrame:CGRectMake(__kScreenWidth-40, 7.5 , 25, 35)] ;
    [nextView2 setImage:[UIImage imageNamed:@"page_access_next_icon.png"]] ;
    [serviceView addSubview:nextView2] ;
    
    //分割线6
    UIImageView *line6 = [[UIImageView alloc] initWithFrame:CGRectMake(10, serviceView.frame.origin.y + serviceView.frame.size.height, __kScreenWidth - 20, 1)] ;
    [line6 setImage:[UIImage imageNamed:@"layout_small_line_bg.png"]] ;
    [scrollView addSubview:line6] ;
    
    saveBtn = [UIButton buttonWithType:UIButtonTypeCustom] ;
    saveBtn.frame = CGRectMake(20, line6.frame.origin.y + line6.frame.size.height + 30, __kScreenWidth - 40, 40) ;
    [saveBtn setBackgroundImage:[UIImage imageNamed:@"button_normal_bg.png"] forState:UIControlStateNormal] ;
    [saveBtn setTitle:@"保存信息" forState:UIControlStateNormal] ;
    [saveBtn addTarget:self action:@selector(save) forControlEvents:UIControlEventTouchUpInside] ;
    //[btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal] ;
    [scrollView addSubview:saveBtn] ;

    
}

#pragma mark - 清除账户
- (void)cleartext
{
    account.text = nil ;
}

- (void)clearidtext
{
    idCard.text = nil ;
}

#pragma mark - 性别选择
- (void)selectMan
{
    sexStatus = 1 ;
    [self changeSexWithStatus:sexStatus] ;
}

- (void)selectWoman
{
    sexStatus = 2 ;
    [self changeSexWithStatus:sexStatus] ;
}

- (void)changeSexWithStatus:(NSInteger) status
{
    if (status == 1) {
        [manBtn setImage:[UIImage imageNamed:@"radiobutton_checked_img.png"] forState:UIControlStateNormal] ;
        [womanBtn setImage:[UIImage imageNamed:@"radiobutton_normal_img.png"] forState:UIControlStateNormal] ;
    }
    else
    {
        [manBtn setImage:[UIImage
            imageNamed:@"radiobutton_normal_img.png"] forState:UIControlStateNormal] ;
        [womanBtn setImage:[UIImage imageNamed:@"radiobutton_checked_img.png"] forState:UIControlStateNormal] ;
    }
}

#pragma mark - 选择所在区域
- (void)pushLTAdress
{
    AdressLTViewController *vc = [[AdressLTViewController alloc] init] ;
    [self.navigationController pushViewController:vc animated:NO] ;
}

#pragma mark - 选择服务区域
- (void)pushService
{
    ServiceLTViewController *vc = [[ServiceLTViewController alloc] init] ;
    [self.navigationController pushViewController:vc animated:NO] ;
}

#pragma mark -保存修改
- (void)save
{
    if (account.text.length == 0) {
        saveBtn.enabled = NO ;
        UILabel *label = [[UILabel alloc] init] ;
        label.font = [UIFont systemFontOfSize:14.0f] ;
        label.text = @"姓名不能为空" ;
        label.textColor = [UIColor whiteColor] ;
        label.textAlignment = NSTextAlignmentCenter ;
        label.backgroundColor = [UIColor blackColor] ;
        label.alpha = 0.8 ;
        label.layer.cornerRadius = 10 ;
        label.layer.masksToBounds = YES ;
        label.frame = CGRectMake((__kScreenWidth - 150) / 2, self.view.frame.size.height - 90, 150, 40) ;
        label.numberOfLines = 0;
        [scrollView addSubview:label] ;
        [self performSelector:@selector(clearbtn:) withObject:label afterDelay:2.0] ;
    }
    else if (idCard.text.length == 0)
    {
        saveBtn.enabled = NO ;
        UILabel *label = [[UILabel alloc] init] ;
        label.font = [UIFont systemFontOfSize:14.0f] ;
        label.text = @"身份证不能为空" ;
        label.textColor = [UIColor whiteColor] ;
        label.textAlignment = NSTextAlignmentCenter ;
        label.backgroundColor = [UIColor blackColor] ;
        label.alpha = 0.8 ;
        label.layer.cornerRadius = 10 ;
        label.layer.masksToBounds = YES ;
        label.frame = CGRectMake((__kScreenWidth - 150) / 2, self.view.frame.size.height - 90, 150, 40) ;
        label.numberOfLines = 0;
        [scrollView addSubview:label] ;
        [self performSelector:@selector(clearbtn:) withObject:label afterDelay:2.0] ;
    }
    else if ( ![self checkIdentityCardNo:idCard.text] )
    {
        saveBtn.enabled = NO ;
        UILabel *label = [[UILabel alloc] init] ;
        label.font = [UIFont systemFontOfSize:14.0f] ;
        label.text = @"身份证格式错误" ;
        label.textColor = [UIColor whiteColor] ;
        label.textAlignment = NSTextAlignmentCenter ;
        label.backgroundColor = [UIColor blackColor] ;
        label.alpha = 0.8 ;
        label.layer.cornerRadius = 10 ;
        label.layer.masksToBounds = YES ;
        label.frame = CGRectMake((__kScreenWidth - 150) / 2, self.view.frame.size.height - 90, 150, 40) ;
        label.numberOfLines = 0;
        [scrollView addSubview:label] ;
        [self performSelector:@selector(clearbtn:) withObject:label afterDelay:2.0] ;
    }
    else if (!_location_model.location1)
    {
        saveBtn.enabled = NO ;
        UILabel *label = [[UILabel alloc] init] ;
        label.font = [UIFont systemFontOfSize:14.0f] ;
        label.text = @"所在区域不能为空" ;
        label.textColor = [UIColor whiteColor] ;
        label.textAlignment = NSTextAlignmentCenter ;
        label.backgroundColor = [UIColor blackColor] ;
        label.alpha = 0.8 ;
        label.layer.cornerRadius = 10 ;
        label.layer.masksToBounds = YES ;
        label.frame = CGRectMake((__kScreenWidth - 150) / 2, self.view.frame.size.height - 90, 150, 40) ;
        label.numberOfLines = 0;
        [scrollView addSubview:label] ;
        [self performSelector:@selector(clearbtn:) withObject:label afterDelay:2.0] ;
    }
    else if (liveadress.text.length == 0)
    {
        saveBtn.enabled = NO ;
        UILabel *label = [[UILabel alloc] init] ;
        label.font = [UIFont systemFontOfSize:14.0f] ;
        label.text = @"详细地址不能为空" ;
        label.textColor = [UIColor whiteColor] ;
        label.textAlignment = NSTextAlignmentCenter ;
        label.backgroundColor = [UIColor blackColor] ;
        label.alpha = 0.8 ;
        label.layer.cornerRadius = 10 ;
        label.layer.masksToBounds = YES ;
        label.frame = CGRectMake((__kScreenWidth - 150) / 2, self.view.frame.size.height - 90, 150, 40) ;
        label.numberOfLines = 0;
        [scrollView addSubview:label] ;
        [self performSelector:@selector(clearbtn:) withObject:label afterDelay:2.0] ;
    }
    else if (!_service_model.location1)
    {
        saveBtn.enabled = NO ;
        UILabel *label = [[UILabel alloc] init] ;
        label.font = [UIFont systemFontOfSize:14.0f] ;
        label.text = @"服务区域不能为空" ;
        label.textColor = [UIColor whiteColor] ;
        label.textAlignment = NSTextAlignmentCenter ;
        label.backgroundColor = [UIColor blackColor] ;
        label.alpha = 0.8 ;
        label.layer.cornerRadius = 10 ;
        label.layer.masksToBounds = YES ;
        label.frame = CGRectMake((__kScreenWidth - 150) / 2, self.view.frame.size.height - 90, 150, 40) ;
        label.numberOfLines = 0;
        [scrollView addSubview:label] ;
        [self performSelector:@selector(clearbtn:) withObject:label afterDelay:2.0] ;
    }
    else{
        NSUserDefaults *uidDefaults = [NSUserDefaults standardUserDefaults] ;
        NSInteger uidInt = [[uidDefaults valueForKey:@"uid"] integerValue] ;
        
        NSNumber *cmd = [[NSNumber alloc] initWithInt:216] ;
        NSNumber *uid = [[NSNumber alloc] initWithInteger:uidInt] ;
        NSNumber *gender = [[NSNumber alloc] initWithInteger:sexStatus] ;
        /*
         "idcard": "20150720000009000012",        //身份证
         "gender":1,                                //性别 1=男  2=女
         "realname":"太上老君",                        //姓名
         "liveLocation1":"AH",                        //居住地域
         "liveLocation2":"00",
         "liveLocation3":"00",
         "serviceLocate1":"BJ",                        //服务地域
         "serviceLocate2":"00",
         "serviceLocate3":"00",
         "liveaddress":"北京市东城区天坛东路甲1号天坛公园内" //居住地址
         */
        
        NSDictionary *jsondic = @{@"idcard":idCard.text,
                                  @"gender":gender,
                                  @"realname":account.text,
                                  @"liveLocation1":_location_model.location1,
                                  @"liveLocation2":_location_model.location2,
                                  @"liveLocation3":_location_model.location3,
                                  @"serviceLocate1":_service_model.location1,
                                  @"serviceLocate2":_service_model.location2,
                                  @"serviceLocate3":_service_model.location3,
                                  @"liveaddress":liveadress.text} ;
        NSLog(@"注册json========%@",jsondic) ;
        
        NSString *jsonstr =  [CodeWithDES encodeWithDic:jsondic] ;
        NSLog(@"%@",jsondic) ;
        NSLog(@"%@",jsonstr) ;
        NSDictionary *parameters = @{@"cmd":cmd, @"uid":uid, @"jsonstr":jsonstr};
        
        NSLog(@"parameters:%@",parameters) ;
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        hud.label.text = NSLocalizedString(@"正在努力加载", @"HUD loading title") ;
        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
        manager.responseSerializer = [AFHTTPResponseSerializer serializer];
        [manager POST:getAppJson parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject)
         {
             [hud hideAnimated:YES] ;
             //解密并解析json数据，将json转成字典
             NSString *decodeStr = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding] ;
             NSLog(@"decodeStr:%@",decodeStr) ;
             
             decodeStr = [CodeWithDES decodeWithStr:decodeStr] ;
             NSLog(@"decodeStr:%@",decodeStr) ;
             NSData *decodeData = [decodeStr dataUsingEncoding:NSUTF8StringEncoding] ;
             id result = [NSJSONSerialization JSONObjectWithData:decodeData options:NSJSONReadingMutableContainers error:nil] ;
             //获取字典中的值
             if ([result isKindOfClass:[NSDictionary class]])
             {
                 NSDictionary *dic = result ;
                 NSLog(@"通知:%@",dic) ;
                 if ([dic[@"status"]integerValue] == 1) {
                     AppDelegate *delegate = (AppDelegate*)[[UIApplication sharedApplication] delegate] ;
                     MainTabBarViewController *vc = [[MainTabBarViewController alloc] init] ;
                     delegate.window.rootViewController = vc ;
                 }
             }
         }
              failure:^(AFHTTPRequestOperation *operation, NSError *error)
         {
             [hud hideAnimated:YES] ;
             NSLog(@"Error: %@", error);
         }];

    }
    
    
}

#pragma mark - 收起键盘
- (void)hideKeyBoard:(id)sender
{
    [account  resignFirstResponder] ;
    [idCard resignFirstResponder] ;
    [liveadress resignFirstResponder] ;
    
    NSTimeInterval animationDuration=0.30f;
    [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
    [UIView setAnimationDuration:animationDuration];
    CGRect rect = CGRectMake(0, 0, __kScreenWidth, __kScreenHeight-60);//上移80个单位，按实际情况设置
    scrollView.frame = rect;
    [UIView commitAnimations];
}

#pragma mark - textfieldDelegate

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder] ;
    
    if ([textField isEqual:liveadress]) {
        NSTimeInterval animationDuration=0.30f;
        [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
        [UIView setAnimationDuration:animationDuration];
        CGRect rect = CGRectMake(0, 0, __kScreenWidth, __kScreenHeight-60);//上移80个单位，按实际情况设置
        scrollView.frame = rect;
        [UIView commitAnimations];
    }
    
    return YES ;
}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    if ([textField isEqual:liveadress]) {
        NSTimeInterval animationDuration=0.30f;
        [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
        [UIView setAnimationDuration:animationDuration];
        CGRect rect = CGRectMake(0, -30, __kScreenWidth, __kScreenHeight-60);//上移80个单位，按实际情况设置
        scrollView.frame = rect;
        [UIView commitAnimations];
    }
    return YES ;
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if (textField == idCard) {
        if (textField.text.length > 18) {
            return NO ;
        }
    }
    return YES ;
}

//创建label
- (UILabel *)creatlabel:(CGFloat) size color:(UIColor *)color
{
    UILabel *label = [[UILabel alloc] init] ;
    label.font = [UIFont systemFontOfSize:size] ;
    label.backgroundColor = [UIColor clearColor] ;
    label.textAlignment = NSTextAlignmentLeft ;
    label.textColor = color ;
    return label ;
}

- (CGSize)lengthwithLabel:(UILabel *)lable withsize:(CGFloat) size
{
    NSDictionary *attribute1 = @{NSFontAttributeName: [UIFont fontWithName:@"AvenirLTStd-Light" size:size]};
    CGSize DescSize = [lable.text boundingRectWithSize:CGSizeMake(MAXFLOAT, size) options: NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:attribute1 context:nil].size;
    return DescSize ;
}

- (void)clearbtn:(id)sender
{
    saveBtn.enabled = YES ;
    [sender removeFromSuperview] ;
}

#pragma mark - 身份证识别
- (BOOL)checkIdentityCardNo:(NSString*)cardNo
{
    if (cardNo.length != 18) {
        return  NO;
    }
    
    NSArray* codeArray = [NSArray arrayWithObjects:@"7",@"9",@"10",@"5",@"8",@"4",@"2",@"1",@"6",@"3",@"7",@"9",@"10",@"5",@"8",@"4",@"2", nil];
    
    NSDictionary* checkCodeDic = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"1",@"0",@"X",@"9",@"8",@"7",@"6",@"5",@"4",@"3",@"2", nil]  forKeys:[NSArray arrayWithObjects:@"0",@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10", nil]];
    
    NSScanner* scan = [NSScanner scannerWithString:[cardNo substringToIndex:17]];
    
    int val;
    BOOL isNum = [scan scanInt:&val] && [scan isAtEnd];
    if (!isNum) {
        return NO;
    }
    int sumValue = 0;
    
    for (int i =0; i<17; i++) {
        sumValue+=[[cardNo substringWithRange:NSMakeRange(i , 1) ] intValue]* [[codeArray objectAtIndex:i] intValue];
    }
    
    NSString* strlast = [checkCodeDic objectForKey:[NSString stringWithFormat:@"%d",sumValue % 11]];
    
    if ([strlast isEqualToString: [[cardNo substringWithRange:NSMakeRange(17, 1)]uppercaseString]]) {
        return YES;
    }
    return  NO;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
